import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";

const useAddMemberStructure = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    // 加载组织所有已认证成员信息
    const addMemberStructure = async (values, operate, callback) => {
        state.loading = true;
        let res = await graphqlFetch({
            query: operate === "create" ? createQuery : updateQuery,
            variables: values,
        });
        callback && callback(res);
        state.loading = false;
        return res;
    };

    return { ...state, addMemberStructure };
};

const createQuery = compress(`mutation MyMutation(
    $omUuid: UUID!
    $isAppr: Boolean!
    $isAudt: Boolean!
    $omLeve: Int!
    $paPath: [String]
    $paUuid: UUID
  ) {
    createMembersStructure(
      input: {
        membersStructure: {
          omUuid: $omUuid
          isAppr: $isAppr
          isAudt: $isAudt
          omLeve: $omLeve
          paPath: $paPath
          paUuid: $paUuid
        }
      }
    ) {
      clientMutationId
    }
}`);

const updateQuery = compress(`mutation MyMutation(
    $omUuid: UUID!
    $isAppr: Boolean!
    $isAudt: Boolean!
    $omLeve: Int!
    $paPath: [String]
    $paUuid: UUID
  ) {
    updateMembersStructureByOmUuid(
      input: {
        membersStructurePatch: {
          isAppr: $isAppr
          isAudt: $isAudt
          omLeve: $omLeve
          paPath: $paPath
          paUuid: $paUuid
        }
        omUuid: $omUuid
      }
    ) {
      clientMutationId
    }
}`);

export default useAddMemberStructure;
